package com.example.eci_quartz.cloudcc.commcon;

import com.example.eci_quartz.cloudcc.dao.baseDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import java.util.HashMap;
import java.util.Map;

public class CommonMethod extends baseDao{
	//验证是否json格式
	public  static boolean isJSONValid(String jsonstr) {
        try {
        	net.sf.json.JSONObject.fromObject(jsonstr);
            return true;
        } catch(Exception e) {
            return false;
        }
    }
	public static String getBingAgain(){
		String binding="";
		for (int i = 0; i <5; i++) {
			if(!"".equals(binding)){
				break;
			}else{
				binding = InsertXMInfo();
			}
		}
		return binding;
	}
    /**
     * 获取CSR系统用户---简档是CSR的用户
     */
    @SuppressWarnings("rawtypes")
    public static Map getUserByName(String binding){
    	String data_query = "serviceName=cquery&objectApiName=ccuser" +
    			"&expressions=isusing='1' and employeenum is not null and profile='aaa2018BE817994JT8Iy' " +
    			" and is_deleted='0'&binding=";//查询系统中启用用户信息
    	String userinfo = "";//查询返回来的值
    	String user_datas = "";//查询成功返回来的数据
    	Map<String, String> userMap =null;
    	try {
    		if("".equals(binding)){
    			binding=getBingAgain();
    		}
    		userMap = new HashMap<String, String>();// map<用户编号，用户对象值>
    		userinfo=operate(data_query+ binding);//通过http接口调用平台的查询方法
    		//判断binding是否失效，如果失效，就重新获取
    		JSONObject fromObject =null;
    		String result="";
    		if(CommonMethod.isJSONValid(userinfo)){
    			fromObject = JSONObject.fromObject(userinfo);
    			result= fromObject.getString("result")==null?"":fromObject.getString("result");
    		}
    		if(!"true".equals(result)){
    			for (int i = 0; i <5; i++) {
    				userinfo=operate(data_query+binding);
    				if(CommonMethod.isJSONValid(userinfo)){
    					fromObject = JSONObject.fromObject(userinfo);
    					result= fromObject.getString("result")==null?"":fromObject.getString("result");
    				}
    				if("true".equals(result)){
    					break;
    				}
    			}
    		}

    		String returninfo = "[" + userinfo + "]";// 手动转换成json格式，方便取值
    		if (!"[]".equals(returninfo)&&userinfo!=null) {
    			JSONArray brand_list = JSONArray.fromObject(returninfo);
    			if (brand_list != null) {
    				for (int j = 0; j < brand_list.size(); j++) {
    					JSONObject brandInfo = (JSONObject) brand_list.get(j);
    					String Result = brandInfo.get("result") == null ? ""
    							: brandInfo.get("result") + "";// 获取返回值
    					String date = brandInfo.get("data") == null ? ""
    							: brandInfo.get("data") + "";// 获取查询结果的返回值
    					if ("true".equals(Result)) {
    						user_datas = date;
    					}
    				}
    			}
    		}
    		// 查询crm系统中客户的信息并存入到map中
			if (!"[]".equals(user_datas)&&!"".equals(user_datas)) {
				JSONArray datas_list = JSONArray.fromObject(user_datas);
				if (datas_list != null) {
					for (int i = 0; i < datas_list.size(); i++) {
						JSONObject accObj = (JSONObject) datas_list.get(i);
						String name = accObj.get("name") == null? "" : accObj.get("name")+ "";
						String id = accObj.get("name") == null? "" : accObj.get("id")+ "";
						if (!"".equals(name)) {
							if (!userMap.containsKey(name)) {
								userMap.put(name,id);
							}
						}
					}
				}
			}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	//System.out.println(userMap.toString());
    	return userMap;
    }
}
